using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP.Modules;
using WP.Commons;
using WP.DB;
using System.Collections;
namespace WP.MF.Form
{
    public partial class frmMUserDftScreenSetting : BaseQryRpt 
    {
        private enum DtlColumn { Username = 0, Desc, ApplyTo }
        public frmMUserDftScreenSetting()
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            FillCboUser();
            InitGridDtl();
            FillDtl();
        }

        private void InitGridDtl()
        {
            this.gdvDtl.Columns.Clear();
            this.gdvDtl.Rows.Clear();
            
            DataGridViewCheckBoxColumn colApply = new DataGridViewCheckBoxColumn();
            colApply.HeaderText = "Apply?";
            colApply.Width = 60;
            colApply.ReadOnly = false;
            colApply.Name = "";
            DataGridViewTextBoxColumn colUserName = new DataGridViewTextBoxColumn();
            colUserName.HeaderText = "User";
            colUserName.Width = 100;
            colUserName.ReadOnly = true;

            DataGridViewTextBoxColumn colDesc = new DataGridViewTextBoxColumn();
            colDesc.HeaderText = "Decription";
            colDesc.Width = 100;
            colDesc.ReadOnly = true;

            this.gdvDtl.Columns.AddRange(new DataGridViewColumn[] { colUserName, colDesc, colApply });

            this.gdvDtl.EditMode = DataGridViewEditMode.EditOnEnter;
            this.gdvDtl.RowTemplate.Height = 20;
            this.gdvDtl.RowHeadersWidth = 23;
            this.gdvDtl.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.gdvDtl.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.gdvDtl.ColumnHeadersHeight = 20;
            this.gdvDtl.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;

        }

        private void FillDtl()
        {
            LoginUtil loginUtil = new LoginUtil();
            string strResourcedir = loginUtil.GetResourcedir(false);
            string strIniFile = strResourcedir + Constants.LOGFILE_NAME;
            this.gdvDtl.Rows.Clear();
            DBAccess dbcon = new DBAccess(false, false);
            string strFromUserName = cboUserName.SelectedItem.ToString();

            DataView dvUser = dbcon.GetDataTable("select * from mUser order by username").DefaultView;
            int i = 0;
            foreach (DataRowView drw in dvUser)
            {
                gdvDtl.Rows.Add();
                gdvDtl.Rows[i].Cells[(int)DtlColumn.Username].Value = drw["UserName"].ToString().TrimEnd();
                gdvDtl.Rows[i].Cells[(int)DtlColumn.Desc].Value = drw["Description"].ToString().TrimEnd();                
                gdvDtl.Rows[i].Cells[(int)DtlColumn.ApplyTo].Value = false;                
                i++;
            }
            dbcon.CloseCon();
        }

        private void FillCboUser()
        {              
            cboUserName.Items.Clear(); 
            DBAccess dbcon = new DBAccess(false, false);
            DataView dvUser = dbcon.GetDataTable("select * from mUser order by username").DefaultView;
            foreach ( DataRowView drw in dvUser )
            {
                cboUserName.Items.Add(CvtFunc.ToString(drw["UserName"])); 
            }
            dbcon.CloseCon();
            cboUserName.SelectedItem = SysVar.UserName; 
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DBAccess dbcon = new DBAccess(false, false);
            if (cboUserName.SelectedItem == null) return;
            string strFromUserName=cboUserName.SelectedItem.ToString();   
            foreach (DataGridViewRow dgvRow in this.gdvDtl.Rows)
            {
                if (CvtFunc .ToBoolean ( dgvRow.Cells[(int)DtlColumn.ApplyTo].Value ) )
                {
                    string strUserName = CvtFunc.ToString(dgvRow.Cells[(int)DtlColumn.Username].Value);
                    if ( strFromUserName.ToUpper()  != strUserName.ToUpper()  )
                    {
                        string strSql = "Delete from mUserFormGridStartStatus where username='" + strUserName + "'";
                        dbcon.ExecuteSql(strSql);

                        strSql = " Insert into mUserFormGridStartStatus (Parameter,SubParameter,FormName,TableName,username,GridName,ColumnWidthString)  " +
                                 " select Parameter,SubParameter,FormName,TableName,'" + strUserName + "',GridName,ColumnWidthString from mUserFormGridStartStatus where username='" + strFromUserName + "' ";
                        dbcon.ExecuteSql(strSql);
                    }
                }
            }
            this.Close();    
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void cboUserName_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvRow in this.gdvDtl.Rows)
            {
                dgvRow.Cells[(int)DtlColumn.ApplyTo].Value = false;   
            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvRow in this.gdvDtl.Rows)
            {
                dgvRow.Cells[(int)DtlColumn.ApplyTo].Value = true;
            }
        }

        private void btnUnSel_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvRow in this.gdvDtl.Rows)
            {
                dgvRow.Cells[(int)DtlColumn.ApplyTo].Value = false;
            }
        }
    }
}